package com.anti.modular.sys.menu.controller;

import com.anti.commom.api.Result;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.commom.annotation.BusinessLog;
import com.anti.modular.sys.menu.param.SysMenuParam;
import com.anti.modular.sys.menu.service.SysMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2020-11-9
 */
@RestController
@RequestMapping("sysMenu")
public class MenuController {
    @Resource
    SysMenuService sysMenuService;


    /**
     * 添加系统菜单
     */
    @PostMapping("add")
    @BusinessLog(title = "系统菜单_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(SysMenuParam.add.class) SysMenuParam sysMenuParam) {
        sysMenuService.add(sysMenuParam);
        return Result.success("菜单新增成功");
    }

    /**
     * 删除系统菜单
     */
    @PostMapping("delete")
    @BusinessLog(title = "系统菜单_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(SysMenuParam.delete.class) SysMenuParam sysMenuParam) {
        sysMenuService.delete(sysMenuParam);
        return Result.success("菜单删除成功");
    }


    /**
     * 编辑系统菜单
     */
    @PostMapping("edit")
    @BusinessLog(title = "系统菜单_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(SysMenuParam.edit.class) SysMenuParam sysMenuParam) {
        sysMenuService.edit(sysMenuParam);
        return Result.success("修改成功");
    }

    /**
     * 系统菜单列表（树）
     */
    @GetMapping("list")
    public Result list(SysMenuParam sysMenuParam) {
        return Result.data(sysMenuService.list(sysMenuParam));
    }

    /**
     * 获取系统菜单树，用于新增，编辑时选择上级节点
     */
    @GetMapping("tree")
    @BusinessLog(title = "系统菜单_树", opType = LogAnnotionOpTypeEnum.TREE)
    public Result tree(SysMenuParam sysMenuParam) {
        return Result.data(sysMenuService.tree(sysMenuParam));
    }

    /**
     * 根据系统切换菜单
     */
    @PostMapping("change")
    @BusinessLog(title = "系统菜单_切换", opType = LogAnnotionOpTypeEnum.TREE)
    public Result change(@RequestBody SysMenuParam sysMenuParam) {
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        return Result.data(sysMenuService.getLoginMenus(sysLoginUserId, sysMenuParam.getApplication()));
    }

    /**
     * 获取系统菜单树，用于给角色授权时选择
     */
    @GetMapping("treeForGrant")
    @BusinessLog(title = "系统菜单_授权树", opType = LogAnnotionOpTypeEnum.TREE)
    public Result treeForGrant(SysMenuParam sysMenuParam) {
        return Result.data(sysMenuService.treeForGrant(sysMenuParam));
    }
}